﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YogaStudio.Data.Domain;
using YogaStudio.Data;

namespace YogaStudio.Service.BLL
{
    public class FacultyService : YogaStudio.Service.BLL.IFacultyService
    {
        private YogaStudioEntities _db = new YogaStudioEntities();
        public IEnumerable<WaiverRequest> GetWaiverRequestsByFacultyId(int facultyId)
        {
            return _db.WaiverRequests.Where(w => w.FacultyId == facultyId);
        }

        public Faculty GetFacultyByLogInUserName(string userName)
        {
            IUserManagementService userService = new UserManagementService();
            var user = userService.GetUser(userName);
            return _db.Faculties.SingleOrDefault(f => f.User.UserId == user.UserId);
        }

        public IEnumerable<Customer> GetAdviseesByFacultyId(int facultyId)
        {
            return _db.Customers.Where(c => c.Advisor.Id == facultyId);
        }

        public IEnumerable<Customer> GetAllAdvisees()
        {
            return _db.Customers.Where(c => c.Advisor != null);
        }

        public Faculty GetFreeFaculty()
        {
            return _db.Faculties.Select(f => new
            {
                Faculty = f,
                NumberOfAdvisees = _db.Customers.Count(c => c.Advisor == f)
            }).OrderBy(o => o.NumberOfAdvisees)
            .First().Faculty;
        }

        public void Dispose(bool disposing)
        {
            if (disposing) _db.Dispose();
        }

        public void Dispose()
        {
            Dispose(true);
        }
    }
}
